<?php

$sqlF = sprintf("SELECT script FROM Page_mount WHERE Tabela='%s' GROUP BY script ORDER BY script", $TABLE);
$resultF = mysql_query($sqlF, $dbctl);
if($resultF == 0)
{
     printf("<h1>(PrepAjax-1)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($resultF) > 0)
     {
          while($myrowF = mysql_fetch_row($resultF))
          {
               $n = 0;
               $sql = sprintf("SELECT campo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha LIMIT 2", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-2)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              $campo[$n++] = $myrow[0];
                              //printf("<p>campo%d:%s</p>",$n,$myrow[0]);
                         }
                    }
               }

               $ajax = "<?php\n";
               $ajax .= "session_start();\n";

               //$ajax .= sprintf("\$filename = \"../my_inc/Cad%s.inc.php\";\n", ucfirst($TABLE));
               $ajax .= sprintf("\$filename = \"../my_inc/Cad%s.inc.php\";\n", ucfirst($myrowF[0]));
               $ajax .= "if(file_exists(\$filename))\n";
               $ajax .= "{\n";
               //$ajax .= sprintf("     include \"../my_inc/Cad%s.inc.php\";\n", ucfirst($TABLE));
               $ajax .= sprintf("     include \"../my_inc/Cad%s.inc.php\";\n", ucfirst($myrowF[0]));
               $ajax .= "}\n";
               $ajax .= "include '../conexao.inc.php';\n";
               $ajax .= "include '../chk_integridade.inc.php';\n";

               $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-3)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if(substr(strtolower($myrow[0]), 0, 2) == "id")
                              {
                                   //$sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                                   $sel = $myrow[0];
                                   if(strstr($sel, "_id") != "")
                                   {
                                        $tb2 = strstr($sel, "_id");
                                        $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                                        $tb1 = ucfirst(substr($sel, 2, strlen($sel) - 2));
                                        $tb2 = substr($tb2, 1, strlen($tb2) - 1);
                                        $tb3 = ucfirst(substr($tb2, 2, strlen($tb2) - 2));
                                        //printf("<p>tb2:%s</p>",$tb2);
                                        $ret = GetCampo1($db, $myrow[0]);
                                        //printf("<p>campo:%s  sel:%s</p>",$ret,$sel);
                                        $ajax .= sprintf("if(isset(\$_REQUEST['%s']))\n", strtoupper($tb2));
                                        $ajax .= sprintf("{\n");
                                        $ajax .= sprintf("     if(isset(\$_REQUEST['POPUP']))\n");
                                        $ajax .= sprintf("     {\n");

                                        $ajax .= sprintf("          \$sql = \"SELECT \" . \$_REQUEST['POPUP'] . \" FROM %s WHERE %s ='\" . \$_REQUEST['%s'] . \"'\";\n", $tb3, strtolower($tb2), strtoupper($tb2));
                                        $ajax .= sprintf("          \$result = mysql_query(\$sql, \$db);\n");
                                        $ajax .= sprintf("          printf(\"[\");\n");
                                        $ajax .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                                        $ajax .= sprintf("          {\n");
                                        if(isset($_REQUEST['ISO']))
                                        {
                                             $ajax .= sprintf("               \$myrow[0] = iconv('iso-8859-1', 'utf-8', \$myrow[0]);\n");
                                        }
                                        $ajax .= sprintf("               \$myrow[0] = str_replace(\"\\n\",\"+++\",\$myrow[0]);\n");
                                        $ajax .= sprintf("               \$myrow[0] = str_replace(\"\\r\",\"---\",\$myrow[0]);\n");
                                        $ajax .= sprintf("               printf(\"{ \\\"popupValue\\\": \\\"POPUP\\\", \\\"popupDisplay\\\": \\\"\$myrow[0]\\\" }\");\n");
                                        $ajax .= sprintf("          }\n");
                                        $ajax .= sprintf("          printf(\"]\n\");\n");



                                        $ajax .= sprintf("     }\n");
                                        $ajax .= sprintf("     else\n");
                                        $ajax .= sprintf("     {\n");
                                        $ajax .= sprintf("          \$sql = \"SELECT %s,%s FROM %s WHERE %s ='\" . \$_REQUEST['%s'] . \"' ORDER BY %s\";\n", $sel, $ret, $tb1, strtolower($tb2), strtoupper($tb2), $ret);
                                        $ajax .= sprintf("          \$result = mysql_query(\$sql, \$db);\n");
                                        $ajax .= sprintf("          printf(\"[\");\n");
                                        $ajax .= sprintf("          \$n = 0;\n");
                                        $ajax .= sprintf("          \$fields = mysql_num_fields(\$result);\n");
                                        $ajax .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                                        $ajax .= sprintf("          {\n");
                                        if(isset($_REQUEST['ISO']))
                                        {
                                             $ajax .= sprintf("               \$myrow[1] = iconv('iso-8859-1', 'utf-8', \$myrow[1]);\n");
                                        }
                                        $ajax .= sprintf("               if(\$n == 0)\n");
                                        $ajax .= sprintf("               {\n");
                                        $ajax .= sprintf("                    printf(\"{ \\\"optionValue\\\": \\\"\$myrow[0]\\\", \\\"optionDisplay\\\": \\\"\$myrow[1]\\\" }\");\n");
                                        $ajax .= sprintf("               }\n");
                                        $ajax .= sprintf("               else\n");
                                        $ajax .= sprintf("               {\n");
                                        $ajax .= sprintf("                    printf(\",{ \\\"optionValue\\\": \\\"\$myrow[0]\\\", \\\"optionDisplay\\\": \\\"\$myrow[1]\\\" }\");\n");
                                        $ajax .= sprintf("               }\n");
                                        $ajax .= sprintf("               \$n++;\n");
                                        $ajax .= sprintf("          }\n");
                                        $ajax .= sprintf("     printf(\"]\\n\");\n");
                                        $ajax .= sprintf("     }\n");
                                        $ajax .= sprintf("}\n");
                                   }
                              }
                              if($myrow[1] == "mediumblob")
                              {
                                   $ajax .= sprintf("if(isset(\$_REQUEST['get%s']))\n", strtoupper($myrow[0]));
                                   $ajax .= "{\n";
                                   $ajax .= sprintf("     \$diretorio = \"../%s_%s\";\n", strtolower($TABLE), strtolower($myrow[0]));
                                   $ajax .= sprintf("     \$name_th = sprintf(\"th_%%06d.jpg\", \$_REQUEST['get%s']);\n", strtoupper($myrow[0]));
                                   $ajax .= sprintf("     \$path_th = \$diretorio . \"/\" . \$name_th;\n");
                                   $ajax .= sprintf("     if(is_file(\$path_th))\n");
                                   $ajax .= sprintf("     {\n");
                                   $ajax .= sprintf("          \$fd = fopen(\$path_th, \"rb\");\n");
                                   $ajax .= sprintf("          if(\$fd)\n");
                                   $ajax .= sprintf("          {\n");
                                   $ajax .= sprintf("               \$img = \"\";\n");
                                   $ajax .= sprintf("               while(!feof(\$fd))\n");
                                   $ajax .= sprintf("               {\n");
                                   $ajax .= sprintf("                    \$img .= fread(\$fd, 1024);\n");
                                   $ajax .= sprintf("               }\n");
                                   $ajax .= sprintf("               fclose(\$fd);\n");
                                   $ajax .= sprintf("               header(\"Content-type: image/jpeg\");\n");
                                   $ajax .= sprintf("               echo \$img;\n");
                                   $ajax .= sprintf("          }\n");
                                   $ajax .= sprintf("     }\n");
                                   $ajax .= sprintf("     else");
                                   $ajax .= sprintf("     {\n");
                                   $ajax .= sprintf("          \$path_th = \$diretorio . \"/vazio.jpg\";\n");
                                   $ajax .= sprintf("          \$fd = fopen(\$path_th, \"rb\");\n");
                                   $ajax .= sprintf("          if(\$fd)\n");
                                   $ajax .= sprintf("          {\n");
                                   $ajax .= sprintf("               \$img = \"\";\n");
                                   $ajax .= sprintf("               while(!feof(\$fd))\n");
                                   $ajax .= sprintf("               {\n");
                                   $ajax .= sprintf("                    \$img .= fread(\$fd, 1024);\n");
                                   $ajax .= sprintf("               }\n");
                                   $ajax .= sprintf("               fclose(\$fd);\n");
                                   $ajax .= sprintf("               header(\"Content-type: image/jpeg\");\n");
                                   $ajax .= sprintf("               echo \$img;\n");
                                   $ajax .= sprintf("          }\n");
                                   $ajax .= sprintf("     }\n");

                                   //$ajax .= sprintf("     \$sql = \"SELECT %s FROM %s WHERE id%s='\" . \$_REQUEST['get%s'] . \"'\";\n", strtoupper($myrow[0]), $TABLE, ucfirst($TABLE), strtoupper($myrow[0]));
                                   //$ajax .= "     \$result = mysql_query(\$sql, \$db);\n";
                                   //$ajax .= "     \$myrow = mysql_fetch_row(\$result);\n";
                                   //$ajax .= "     if(\$myrow)\n";
                                   //$ajax .= "     {\n";
                                   //$ajax .= "          header(\"Content-type: image/jpeg\");\n";
                                   //$ajax .= "          echo \$myrow[0];\n";
                                   //$ajax .= "     }\n";
                                   $ajax .= "     exit;\n";
                                   $ajax .= "}\n";
                              }
                         }
                    }
               }
               if($_REQUEST['Tabela'] == 'Jqgrid')
               {
                    $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Lista > '0' ORDER BY Lista", $TABLE, $myrowF[0]);
                    $result = mysql_query($sql, $dbctl);
                    if($result == 0)
                    {
                         printf("<h1>Falha: %s</h1>\n", mysql_error($dbctl));
                         printf("<h3>%s</h3>\n", $sql);
                         exit;
                    }
                    else
                    {
                         if(mysql_num_rows($result) > 0)
                         {
                              $a='';
                              $b='';
                              $c='';
                              $d='';
                              $select = sprintf("SELECT id%s as id,", strtolower($TABLE));
                              prepSelect($dbctl, $TABLE, $myrowF[0],$a,$b,$c,$d);
                              $select = $a . $b;
                              $selectcount = sprintf("SELECT COUNT(*) as count%s", $b);
                         }
                    }
                    $list = $d;
                    $ajax .= "if(isset(\$_REQUEST['rows']))\n";
                    $ajax .= "{\n";
                    $ajax .= "     \$qtype = 1;\n";
                    $ajax .= "     \$where = \"".$c."\";\n";
                    $ajax .= "     \$page = \$_REQUEST['page'];\n";
                    $ajax .= "     \$limit = \$_REQUEST['rows'];\n";
                    $ajax .= "     \$sidx = \$_REQUEST['sidx'];\n";
                    $ajax .= "     \$sord = \$_REQUEST['sord'];\n";
                    $ajax .= "     \$where1 = '';\n";
                    $ajax .= "     if(isset(\$_REQUEST['filters']))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          if(\$_REQUEST['filters'] != '')\n";
                    $ajax .= "          {\n";
                    $ajax .= "               \$filter = json_decode(\$_REQUEST['filters'], true);\n";
                    $ajax .= "               if(count(\$filter['rules']) > 0)\n";
                    $ajax .= "               {\n";
                    $ajax .= "                    \$where1 = \"AND (\";\n";
                    $ajax .= "                    for(\$n = 0; \$n < count(\$filter['rules']); \$n++)\n";
                    $ajax .= "                    {\n";
                    $ajax .= "                         if(\$n > 0)\n";
                    $ajax .= "                         {\n";
                    $ajax .= "                              \$where1 .= \" \" . \$filter['groupOp'] . \" \";\n";
                    $ajax .= "                         }\n";
                    $ajax .= "                         \$where1 .= \$filter['rules'][\$n]['field'] . \" LIKE _utf8 '\" . \$filter['rules'][\$n]['data'] . \"%%' collate utf8_unicode_ci\";\n";
                    $ajax .= "                    }\n";
                    $ajax .= "                    \$where1 .= \")\";\n";
                    $ajax .= "               }\n";
                    $ajax .= "          }\n";
                    $ajax .= "     }\n";
                    $ajax .= "     if(isset(\$_REQUEST['_search']))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          if(\$_REQUEST['_search'] == 'true')\n";
                    $ajax .= "          {\n";
                    $ajax .= "               if(isset(\$_REQUEST['searchField']))\n";
                    $ajax .= "               {\n";
                    $ajax .= "                    \$where .= \" AND \" . \$_REQUEST['searchField'] . ' LIKE _utf8 ' . \"'%\" . \$_REQUEST['searchString'] . \"%' collate utf8_unicode_ci\";\n";
                    $ajax .= "               }\n";
                    $ajax .= "          }\n";
                    $ajax .= "     }\n";
                    $ajax .= "     if(isset(\$_REQUEST['q']))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$qtype = \$_REQUEST['q'];\n";
                    $ajax .= "     }\n";
                    $ajax .= "     if(!\$sidx)\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$sidx = 1;\n";
                    $ajax .= "     }\n";
                    $ajax .= "     \$SQL = \"" . $selectcount . " \$where \$where1\";\n";
                    $ajax .= "     \$result = mysql_query(\$SQL);\n";
                    $ajax .= "     \$row = mysql_fetch_array(\$result, MYSQL_ASSOC);\n";
                    $ajax .= "     \$count = \$row['count'];\n";
                    $ajax .= "     if(\$count > 0)\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$total_pages = ceil(\$count / \$limit);\n";
                    $ajax .= "     }\n";
                    $ajax .= "     else\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$total_pages = 0;\n";
                    $ajax .= "     }\n";
                    $ajax .= "     if(\$page > \$total_pages)\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$page = \$total_pages;\n";
                    $ajax .= "     }\n";
                    $ajax .= "     \$start = \$limit * \$page - \$limit;\n";
                    $ajax .= "     if(\$start < 0)\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$start = 0;\n";
                    $ajax .= "     }\n";
                    $ajax .= "     \$SQL = \"" . $select . " \$where \$where1 ORDER BY \$sidx \$sord LIMIT \$start , \$limit\";\n";
                    $ajax .= "     \$result = mysql_query(\$SQL) or die(\"Falha na execucao da query\" . mysql_error());\n";
                    $ajax .= "     \$responce->page = \$page;\n";
                    $ajax .= "     \$responce->total = \$total_pages;\n";
                    $ajax .= "     \$responce->records = \$count;\n";
                    $ajax .= "     \$i = 0;\n";
                    $ajax .= "     while(\$row = mysql_fetch_array(\$result, MYSQL_ASSOC))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$responce->rows[\$i]['id'] = \$row[id];\n";
                    $ajax .= "          \$responce->rows[\$i]['cell'] = array(".$list.");\n";
                    $ajax .= "          \$i++;\n";
                    $ajax .= "     }\n";
                    $ajax .= "     echo json_encode(\$responce);\n";
                    $ajax .= "}\n";
               }
               else
               {
                    $ajax .= "if(isset(\$_GET['iDisplayStart']))\n";
                    $ajax .= "{\n";

                    $chave = sprintf("id%s", strtolower($TABLE));
                    $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Lista > '0' ORDER BY Lista", $TABLE, $myrowF[0]);
                    $result = mysql_query($sql, $dbctl);
                    if($result == 0)
                    {
                         printf("<h1>(PrepAjax-4)Falha: %s</h1>\n", mysql_error($dbctl));
                         printf("<h3>%s</h3>\n", $sql);
                         exit;
                    }
                    else
                    {
                         if(mysql_num_rows($result) > 0)
                         {
                              $n = 0;
                              $tbl = "";
                              $wh = "";
                              $ajax .= "     \$aColumns = array(";
                              while($myrow = mysql_fetch_row($result))
                              {
                                   if(substr(strtolower($myrow[0]), 0, 2) == "id")
                                   {
                                        $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                                        if(strstr($sel, "_id") != "")
                                        {
                                             $tb2 = strstr($sel, "_id");
                                             $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                                        }

                                        if(strstr($sel, "_") != "")
                                        {
                                             list($first, $second) = explode("_", $sel);
                                             $sel = sprintf("%s_%s", ucfirst($first), ucfirst($second));
                                        }
                                        if(is_numeric(substr($sel, strlen($sel) - 1, 1)))
                                        {
                                             $sel = substr($sel, 0, strlen($sel) - 1);
                                        }

                                        if($sel != ucfirst($TABLE))
                                        {
                                             $cp = GetCampo1($db, $myrow[0]);
                                             if($n == 0)
                                             {
                                                  $ajax .= sprintf("\"%s.%s\"", $sel, $cp);
                                             }
                                             else
                                             {
                                                  $ajax .= sprintf(",\"%s.%s\"", $sel, $cp);
                                             }
                                             if(strstr($tbl, $sel) == "")
                                             {
                                                  $tbl .= ",$sel";
                                             }
                                             //$wh .= sprintf(" AND %s.%s=%s.%s", $sel, $myrow[0], $TABLE, $myrow[0]);
                                             $wh .= sprintf(" AND %s.id%s=%s.%s", $sel, $sel, $TABLE, $myrow[0]);
                                             $n++;
                                             continue;
                                        }
                                   }
                                   if($myrow[1] == "mediumblob")
                                   {
                                        continue;
                                   }
                                   else if($myrow[1] == "date")
                                   {
                                        if($n == 0)
                                        {
                                             $ajax .= sprintf("\"DATE_FORMAT(%s.%s,'%%d/%%m/%%Y')\"", $TABLE, $myrow[0]);
                                        }
                                        else
                                        {
                                             $ajax .= sprintf(",\"DATE_FORMAT(%s.%s,'%%d/%%m/%%Y')\"", $TABLE, $myrow[0]);
                                        }
                                   }
                                   else if($myrow[1] == "datetime")
                                   {
                                        if($n == 0)
                                        {
                                             $ajax .= sprintf("\"DATE_FORMAT(%s.%s,'%%d/%%m/%%Y %%H:%%i')\"", $TABLE, $myrow[0]);
                                        }
                                        else
                                        {
                                             $ajax .= sprintf(",\"DATE_FORMAT(%s.%s,'%%d/%%m/%%Y %%H:%%i')\"", $TABLE, $myrow[0]);
                                        }
                                   }
                                   else
                                   {
                                        if($n == 0)
                                        {
                                             $ajax .= "\"" . $TABLE . "." . $myrow[0] . "\"";
                                        }
                                        else
                                        {
                                             $ajax .= ",\"" . $TABLE . "." . $myrow[0] . "\"";
                                        }
                                   }
                                   $n++;
                              }
                              $ajax .=");\n";
                         }
                    }

                    $ajax .= "     \$sIndexColumn = \"" . $chave . "\";\n";

                    $ajax .= "     \$sTable = \"" . $TABLE . $tbl . "\";\n";
                    $ajax .= "     \$sTable1 = \"" . $TABLE . "\";\n";
                    $ajax .= "     \$sLimit = \"\";\n";
                    $ajax .= "     if(isset(\$_GET['iDisplayStart']) && \$_GET['iDisplayLength'] != '-1')\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$sLimit = \"LIMIT \" . mysql_real_escape_string(\$_GET['iDisplayStart']) . \", \" .\n";
                    $ajax .= "                  mysql_real_escape_string(\$_GET['iDisplayLength']);\n";
                    $ajax .= "     }\n";
                    $ajax .= "     if(isset(\$_GET['iSortCol_0']))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$sOrder = 'ORDER BY  ';\n";
                    $ajax .= "          for(\$i = 0; \$i < intval(\$_GET['iSortingCols']); \$i++)\n";
                    $ajax .= "          {\n";
                    $ajax .= "               if(\$_GET['bSortable_' . intval(\$_GET['iSortCol_' . \$i])] == \"true\")\n";
                    $ajax .= "               {\n";
                    $ajax .= "                    \$sOrder .= \$aColumns[intval(\$_GET['iSortCol_' . \$i])] . \"\n";
                    $ajax .= "				 	\" . mysql_real_escape_string(\$_GET['sSortDir_' . \$i]) . \", \";\n";
                    $ajax .= "               }\n";
                    $ajax .= "          }\n";
                    $ajax .= "          \$sOrder = substr_replace(\$sOrder, \"\", -2);\n";
                    $ajax .= "          if(\$sOrder == \"ORDER BY\")\n";
                    $ajax .= "          {\n";
                    $ajax .= "               \$sOrder = \"\";\n";
                    $ajax .= "          }\n";
                    $ajax .= "     }\n";
                    $ajax .= "     \$sWhere = \"\";\n";
                    $ajax .= "     if(\$_GET['sSearch'] != \"\")\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$sWhere = \"WHERE \";\n";
                    $ajax .= "          for(\$i = 0; \$i < count(\$aColumns); \$i++)\n";
                    $ajax .= "          {\n";
                    $ajax .= "               \$sWhere .= \$aColumns[\$i] . \" LIKE _utf8 '%%\" . mysql_real_escape_string(\$_GET['sSearch']) . \"%%' OR \";\n";
                    $ajax .= "          }\n";
                    $ajax .= "          \$sWhere = substr_replace(\$sWhere, \"\", -3);\n";
                    $ajax .= "          \$sWhere .= '';\n";
                    $ajax .= "     }\n";
                    $ajax .= "     for(\$i = 0; \$i < count(\$aColumns); \$i++)\n";
                    $ajax .= "     {\n";
                    $ajax .= "          if(\$_GET['bSearchable_' . \$i] == 'true' && \$_GET['sSearch_' . \$i] != '')\n";
                    $ajax .= "          {\n";
                    $ajax .= "               if(\$sWhere == \"\")\n";
                    $ajax .= "               {\n";
                    $ajax .= "                    \$sWhere = \"WHERE \";\n";
                    $ajax .= "               }\n";
                    $ajax .= "               else\n";
                    $ajax .= "               {\n";
                    $ajax .= "                    \$sWhere .= \" AND \";\n";
                    $ajax .= "               }\n";
                    $ajax .= "               \$sWhere .= \$aColumns[\$i] . \" LIKE _utf8 '%%\" . mysql_real_escape_string(\$_GET['sSearch_' . \$i]) . \"%%' \";\n";
                    $ajax .= "          }\n";
                    $ajax .= "     }\n";
                    $ajax .= "     if(\$sWhere== \"\")\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$sWhere = \"WHERE 1\";\n";
                    $ajax .= "     }\n";
                    $ajax .= "     \$sQuery = \"SELECT SQL_CALC_FOUND_ROWS \" . str_replace(\" , \", \" \", implode(\", \", \$aColumns)) . \" FROM \$sTable \$sWhere" . $wh . " \$sOrder collate utf8_unicode_ci \$sLimit\";\n";
                    //$ajax .= "     printf(\"<p>%s</p>\",\$sQuery);\n";
                    $ajax .= "     \$rResult = mysql_query(\$sQuery, \$db) or die(mysql_error());\n";
                    $ajax .= "     \$sQuery = 'SELECT FOUND_ROWS()';\n";
                    //$ajax .= "     printf(\"<p>%s</p>\",\$sQuery);\n";
                    $ajax .= "     \$rResultFilterTotal = mysql_query(\$sQuery, \$db) or die(mysql_error());\n";
                    $ajax .= "     \$aResultFilterTotal = mysql_fetch_array(\$rResultFilterTotal);\n";
                    $ajax .= "     \$iFilteredTotal = \$aResultFilterTotal[0];\n";
                    $ajax .= "     \$sQuery = \"SELECT COUNT(\" . \$sIndexColumn . \") FROM \$sTable1\";\n";
                    //$ajax .= "     printf(\"<p>%s</p>\",\$sQuery);\n";
                    //$ajax .= "     exit;\n";
                    $ajax .= "     \$rResultTotal = mysql_query(\$sQuery, \$db) or die(mysql_error());\n";
                    $ajax .= "     \$aResultTotal = mysql_fetch_array(\$rResultTotal);\n";
                    $ajax .= "     \$iTotal = \$aResultTotal[0];\n";
                    $ajax .= "     \$output = array(\n";
                    $ajax .= "         \"sEcho\"=>intval(\$_GET['sEcho']),\n";
                    $ajax .= "         \"iTotalRecords\"=>\$iTotal,\n";
                    $ajax .= "         \"iTotalDisplayRecords\"=>\$iFilteredTotal,\n";
                    $ajax .= "         \"aaData\"=>array()\n";
                    $ajax .= "     );\n";
                    $ajax .= "     while(\$aRow = mysql_fetch_array(\$rResult))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$row = array();\n";
                    $ajax .= "          for(\$i = 0; \$i < count(\$aColumns); \$i++)\n";
                    $ajax .= "          {\n";
                    if(isset($_REQUEST['ISO']))
                    {
                         $ajax .= "               \$aRow[\$i] = iconv('iso-8859-1', 'utf-8', \$aRow[\$i]);\n";
                    }
                    $ajax .= "               \$row[] = \$aRow[\$i];\n";
                    $ajax .= "          }\n";
                    $ajax .= "          \$output['aaData'][] = \$row;\n";
                    $ajax .= "     }\n";
                    $ajax .= "     echo json_encode(\$output);\n";
                    $ajax .= "     return;\n";
                    $ajax .= "}\n";

                    $ajax .= "if(isset(\$_REQUEST['LIST']))\n";
                    $ajax .= "{\n";
                    $ajax .= "     \$regs = 0;\n";
                    $ajax .= "     \$linhas = 15;\n";
                    $ajax .= "     \$sql = \"SELECT COUNT(*) FROM $TABLE\";\n";
                    $ajax .= "     \$result = mysql_query(\$sql,\$db);\n";
                    $ajax .= "     while(\$myrow = mysql_fetch_row(\$result))\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$regs = \$myrow[0];\n";
                    $ajax .= "     }\n";


                    $select = sprintf("\$sql = \"SELECT ");

                    $first = 0;
                    $nlast = 0;
                    $chave = sprintf("id%s", strtolower($TABLE));
                    $sql = sprintf("SELECT campo,tipo,tamanho,label,linha,coluna FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Lista > '0' ORDER BY Lista", $TABLE, $myrowF[0]);
                    $result = mysql_query($sql, $dbctl);
                    if($result == 0)
                    {
                         printf("<h1>(PrepAjax-5)Falha: %s</h1>\n", mysql_error($dbctl));
                         printf("<h3>%s</h3>\n", $sql);
                         exit;
                    }
                    else
                    {
                         if(mysql_num_rows($result) > 0)
                         {
                              while($myrow = mysql_fetch_row($result))
                              {
                                   $tipo = strtolower($myrow[1]);
                                   $size = $myrow[2];
                                   if($tipo == "mediumblob")
                                   {
                                        continue;
                                   }
                                   else if($tipo == "date")
                                   {
                                        if($first == 0)
                                        {
                                             $select .= sprintf("DATE_FORMAT(%s,'%%d/%%m/%%Y') as %s", $myrow[0], $myrow[0]);
                                        }
                                        else
                                        {
                                             $select .= sprintf(",DATE_FORMAT(%s,'%%d/%%m/%%Y') as %s", $myrow[0], $myrow[0]);
                                        }
                                   }
                                   else if($tipo == "datetime")
                                   {
                                        if($first == 0)
                                        {
                                             $select .= sprintf("DATE_FORMAT(%s,'%%d/%%m/%%Y %%H:%%i') as %s", $myrow[0], $myrow[0]);
                                        }
                                        else
                                        {
                                             $select .= sprintf(",DATE_FORMAT(%s,'%%d/%%m/%%Y %%H:%%i') as %s", $myrow[0], $myrow[0]);
                                        }
                                   }
                                   else
                                   {
                                        if($first == 0)
                                        {
                                             $select .= sprintf("%s", $myrow[0]);
                                        }
                                        else
                                        {
                                             $select .= sprintf(",%s", $myrow[0]);
                                        }
                                   }
                                   $first = 1;
                                   $nlast++;
                              }
                         }
                    }
                    $select .= sprintf(",id%s", strtolower($TABLE));
                    $select .= sprintf(" FROM %s", $TABLE);

                    $ajax .= "     if(\$_REQUEST['POS'] == \"first\")\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$p = \$regs - \$linhas;\n";
                    $ajax .= "          if(\$p < 0)\n";
                    $ajax .= "          {\n";
                    $ajax .= "               \$p = 0;\n";
                    $ajax .= "          }\n";

                    $ajax .= sprintf("          %s ORDER BY id%s DESC LIMIT \$p,\$linhas\";\n", $select, strtolower($TABLE));
                    $ajax .= "     }\n";
                    $ajax .= "     else if(\$_REQUEST['POS'] == \"last\")\n";
                    $ajax .= "     {\n";
                    $ajax .= sprintf("          %s ORDER BY id%s DESC LIMIT \$linhas\";\n", $select, strtolower($TABLE));
                    $ajax .= "     }\n";
                    $ajax .= "     else if(\$_REQUEST['POS'] == \"before\")\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$p = \$_REQUEST['ID'] - \$linhas;\n";
                    $ajax .= "          if(\$p < 0)\n";
                    $ajax .= "          {\n";
                    $ajax .= "               \$p = 0;\n";
                    $ajax .= "          }\n";
                    $ajax .= sprintf("          %s WHERE id%s<='\" . \$p . \"' ORDER BY id%s DESC LIMIT \$linhas\";\n", $select, strtolower($TABLE), strtolower($TABLE));
                    $ajax .= "     }\n";
                    $ajax .= "     else if(\$_REQUEST['POS'] == \"after\")\n";
                    $ajax .= "     {\n";
                    $ajax .= "          \$p = \$_REQUEST['ID'] + \$linhas;\n";
                    $ajax .= "          if(\$p > \$regs)\n";
                    $ajax .= "          {\n";
                    $ajax .= "               \$p = \$regs;\n";
                    $ajax .= "          }\n";
                    $ajax .= sprintf("          %s WHERE id%s<='\" . \$p . \"' ORDER BY id%s DESC LIMIT \$linhas\";\n", $select, strtolower($TABLE), strtolower($TABLE));
                    $ajax .= "     }\n";
                    $ajax .= sprintf("     \$result = mysql_query(\$sql,\$db);\n");
                    $ajax .= sprintf("     if(mysql_num_rows(\$result) > 0)\n");
                    $ajax .= sprintf("     {\n");
                    $ajax .= sprintf("          \$linha = 1;\n");
                    $ajax .= sprintf("          \$last_lin = 0;\n");
                    $ajax .= sprintf("          printf(\"[\");\n");
                    $ajax .= sprintf("          \$fields = mysql_num_fields(\$result);\n");
                    $ajax .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                    $ajax .= sprintf("          {\n");
                    $ajax .= sprintf("               for(\$n=0;\$n<\$fields;\$n++)\n");
                    $ajax .= sprintf("               {\n");
                    $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"%%\",\"%%%%\",\$myrow[\$n]);\n");
                    $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"\\n\",\"+++\",\$myrow[\$n]);\n");
                    $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"\\r\",\"---\",\$myrow[\$n]);\n");
                    $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"\\\"\",\"\",\$myrow[\$n]);\n");
                    $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"'\",\"\",\$myrow[\$n]);\n");
                    if(isset($_REQUEST['ISO']))
                    {
                         $ajax .= sprintf("                    \$myrow[\$n] = iconv('iso-8859-1', 'utf-8', \$myrow[\$n]);\n");
                    }
                    $ajax .= sprintf("                    \$myrow[\$n] = trim(\$myrow[\$n]);\n");
                    $ajax .= sprintf("               }\n");
                    $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Lista > '0' ORDER BY Lista", $TABLE, $myrowF[0]);
                    //printf("<p>%s</p>",$sql);
                    $result = mysql_query($sql, $dbctl);
                    if($result == 0)
                    {
                         printf("<h1>(PrepAjax-6)Falha: %s</h1>\n", mysql_error($dbctl));
                         printf("<h3>%s</h3>\n", $sql);
                         exit;
                    }
                    else
                    {
                         if(mysql_num_rows($result) > 0)
                         {
                              $m = 0;
                              while($myrow = mysql_fetch_row($result))
                              {
                                   if(substr(strtolower($myrow[0]), 0, 2) == "id")
                                   {
                                        $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                                        if(strstr($sel, "_id") != "")
                                        {
                                             $tb2 = strstr($sel, "_id");
                                             $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                                        }

                                        if(strstr($sel, "_") != "")
                                        {
                                             list($first, $second) = explode("_", $sel);
                                             $sel = sprintf("%s_%s", ucfirst($first), ucfirst($second));
                                        }
                                        if($sel != ucfirst($TABLE))
                                        {
                                             $ajax .= sprintf("               \$myrow[%d] = Get%s(\$db,\$myrow[%d]);\n", $m, $sel, $m);
                                        }
                                   }
                                   else if(strstr($myrow[1], "double") != "" || strstr($myrow[1], "decimal") != "" || strstr($myrow[1], "float") != "")
                                   {
                                        $ajax .= sprintf("               \$myrow[%d] = number_format(\$myrow[%d],2,',','.');\n", $m, $m);
                                   }
                                   else if(strstr($myrow[1], "mediumblob") != "")
                                   {
                                        continue;
                                   }
                                   if($m == 0)
                                   {
                                        $ajax .= sprintf("               if(\$linha == 1)\n");
                                        $ajax .= sprintf("               {\n");
                                        $ajax .= sprintf("                    printf(\"{ \\\"id\\\": \\\"L%%dC%d\\\", \\\"value\\\": \\\"\$myrow[%d]\\\" }\",\$linha);\n", $m + 1, $m);
                                        $ajax .= sprintf("                    \$last_lin = \$myrow[%d];\n", $nlast);
                                        $ajax .= sprintf("               }\n");
                                        $ajax .= sprintf("               else\n");
                                        $ajax .= sprintf("               {\n");
                                        $ajax .= sprintf("                    printf(\",{ \\\"id\\\": \\\"L%%dC%d\\\", \\\"value\\\": \\\"\$myrow[%d]\\\" }\",\$linha);\n", $m + 1, $m);
                                        $ajax .= sprintf("               }\n");
                                        $m++;
                                        continue;
                                   }
                                   $ajax .= sprintf("               printf(\",{ \\\"id\\\": \\\"L%%dC%d\\\", \\\"value\\\": \\\"\$myrow[%d]\\\" }\",\$linha);\n", $m + 1, $m);
                                   $m++;
                              }
                              $ajax .= sprintf("               \$linha++;\n");
                              $ajax .= sprintf("          }\n");
                              $ajax .= sprintf("          printf(\",{ \\\"id\\\": \\\"last_lin\\\", \\\"value\\\": \\\"\$last_lin\\\" }\");\n");
                              $ajax .= sprintf("          printf(\"]\");\n");
                              $ajax .= sprintf("     }\n");
                              $ajax .= sprintf("     mysql_close(\$db);\n");
                              $ajax .= sprintf("     return;\n");
                              $ajax .= sprintf("}\n");
                         }
                    }
               }
               $ajax .= "if(isset(\$_REQUEST['save']))\n";
               $ajax .= "{\n";
               $ajax .= "     if(\$_REQUEST['save'] == 0)\n";
               $ajax .= "     {\n";
               $ajax .= "          \$valores = \"VALUES('0'\";\n";
               $ajax .= sprintf("          \$campos = \"(id%s\";\n", strtolower($TABLE));

               $FIXOS = array();
               $nFixos = 0;
               $sql = sprintf("SELECT campo,tipo,fixar FROM Page_mount WHERE Tabela='%s' AND script='%s' AND fixar <> '' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-9)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if($myrow[2] != "")
                              {
                                   if($myrow[2] == "SESSION")
                                   {
                                        $ajax .= "          \$valores .= \",'\" . \$_SESSION['IDFUNCIONARIO'] . \"'\";\n";
                                        $ajax .= "          \$campos .= \",$myrow[0]\";\n";
                                   }
                                   if($myrow[2] == "NOW")
                                   {
                                        $ajax .= "          \$valores .= \",NOW()\";\n";
                                        $ajax .= "          \$campos .= \",$myrow[0]\";\n";
                                   }
                                   $FIXOS[$nFixos] = $myrow[0];
                                   $nFixos++;
                              }
                         }
                    }
               }

               $ajax .= "          foreach(\$_REQUEST as \$key => \$val)\n";
               $ajax .= "          {\n";
               $ajax .= "               if(\$key == \"end\")\n";
               $ajax .= "               {\n";
               $ajax .= "                    break;\n";
               $ajax .= "               }\n";
               $ajax .= "               if(\$key == \"save\")\n";
               $ajax .= "               {\n";
               $ajax .= "                    continue;\n";
               $ajax .= "               }\n";
               for($n=0;$n<$nFixos;$n++)
               {
                    $ajax .= "               if(\$key == \"".$FIXOS[$n]."\")\n";
                    $ajax .= "               {\n";
                    $ajax .= "                    continue;\n";
                    $ajax .= "               }\n";
               }
               $sql = sprintf("SELECT campo,tipo,fixar FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' AND Fixar = '' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               //printf("<p>%s</p>",$sql);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-7)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if($myrow[1] == 'enum')
                              {
                                   $ret = checkEnum($db, $TABLE, $myrow[0]);
                                   if($ret == 1)
                                   {
                                        $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                        $ajax .= "               {\n";
                                        $ajax .= "                    \$valores .= \",'V'\";\n";
                                        $ajax .= "                    \$campos .= \",\" . \$key;\n";
                                        $ajax .= "                    continue;\n";
                                        $ajax .= "               }\n";
                                   }
                              }

                              if($myrow[1] == 'datetime' || $myrow[1] == 'date')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = sprintf(\"%s-%s-%s\",substr(\$val,6,4),substr(\$val,3,2),substr(\$val,0,2));\n";
                                   $ajax .= "               }\n";
                              }
                              if($myrow[1] == 'double' || $myrow[1] == 'float' || $myrow[1] == 'decimal')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    if(strstr(\$val, ',') != '')\n";
                                   $ajax .= "                    {\n";
                                   $ajax .= "                         \$val = str_replace('.', '', \$val);\n";
                                   $ajax .= "                    }\n";
                                   $ajax .= "                    \$val = str_replace(',', '.', \$val);\n";
                                   $ajax .= "                    \$val = abs(\$val);\n";
                                   $ajax .= "                    \$val = sprintf(\"%.2f\", floatval(\$val));\n";
                                   $ajax .= "               }\n";
                              }
                              if(strtolower($myrow[0]) == 'cnpj')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = str_replace(\".\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"/\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"-\",\"\",\$val);\n";
                                   $ajax .= "               }\n";
                              }
                              if(strtolower($myrow[0]) == 'cpf')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = str_replace(\".\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"/\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"-\",\"\",\$val);\n";
                                   $ajax .= "               }\n";
                              }
                              if(strtolower($myrow[0]) == 'rg')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = str_replace(\".\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"/\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"-\",\"\",\$val);\n";
                                   $ajax .= "               }\n";
                              }
                         }
                    }
               }
               $ajax .= "               \$val = mysql_real_escape_string(\$val);\n";
               $ajax .= "               \$valores .= \",'\" . \$val . \"'\";\n";
               $ajax .= "               \$campos .= \",\" . \$key;\n";
               $ajax .= "          }\n";

               $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-8)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if($myrow[1] == 'enum')
                              {
                                   $ret = checkEnum($db, $TABLE, $myrow[0]);
                                   if($ret == 1)
                                   {
                                        $ajax .= sprintf("          if(!isset(\$_REQUEST['%s']))\n", strtoupper($myrow[0]));
                                        $ajax .= "          {\n";
                                        $ajax .= "               \$valores .= \",'F'\";\n";
                                        $ajax .= sprintf("               \$campos .= \",%s\";\n", strtoupper($myrow[0]));
                                        $ajax .= "          }\n";
                                   }
                              }
                         }
                    }
               }


               $ajax .= sprintf("          \$sql = \"INSERT INTO %s \$campos) \$valores)\";\n", ucfirst($TABLE));
               //$ajax .= sprintf("          printf(\"<h3>%%s</h3>\n\", \$sql);\n");
               $ajax .= "          \$result = mysql_query(\$sql, \$db);\n";
               $ajax .= "          \$_REQUEST['ID'] = '0';\n";
               $ajax .= "          \$_REQUEST['POS'] = 'last';\n";
               $ajax .= sprintf("          if(file_exists(\"../includes/Cad%s_ajax_insert.php\"))\n", ucfirst($TABLE));
               $ajax .= "          {\n";
               $ajax .= sprintf("               require(\"../includes/Cad%s_ajax_insert.php\");\n", ucfirst($TABLE));
               $ajax .= "          }\n";
               $ajax .= "     }\n";
               $ajax .= "     else\n";
               $ajax .= "     {\n";
               $ajax .= "          \$first = 0;\n";
               $ajax .= sprintf("          \$sql = \"UPDATE %s SET \";\n", ucfirst($TABLE));

               $sql = sprintf("SELECT campo,tipo,fixar FROM Page_mount WHERE Tabela='%s' AND script='%s' AND fixar <> '' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-9)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if($myrow[2] != "")
                              {
                                   if($myrow[2] == "SESSION")
                                   {
                                        $ajax .= "          if(\$first == 0)\n";
                                        $ajax .= "          {\n";
                                        $ajax .= "               \$sql .= \"$myrow[0]='\" . \$_SESSION['IDFUNCIONARIO'] . \"'\";\n";
                                        $ajax .= "          }\n";
                                        $ajax .= "          else\n";
                                        $ajax .= "          {\n";
                                        $ajax .= "               \$sql .= \",$myrow[0]='\" . \$_SESSION['IDFUNCIONARIO'] . \"'\";\n";
                                        $ajax .= "          }\n";
                                        $ajax .= "          \$first++;\n";
                                   }
                                   /*
                                   if($myrow[2] == "NOW")
                                   {
                                        $ajax .= "          if(\$first == 0)\n";
                                        $ajax .= "          {\n";
                                        $ajax .= "               \$sql .= \"$myrow[0]=NOW()\";\n";
                                        $ajax .= "          }\n";
                                        $ajax .= "          else\n";
                                        $ajax .= "          {\n";
                                        $ajax .= "               \$sql .= \",$myrow[0]=NOW()\";\n";
                                        $ajax .= "          }\n";
                                        $ajax .= "          \$first++;\n";
                                   }
                                    */
                                   continue;
                              }
                         }
                    }
               }

               $ajax .= "          foreach(\$_REQUEST as \$key => \$val)\n";
               $ajax .= "          {\n";
               $ajax .= "               if(\$key == \"end\")\n";
               $ajax .= "               {\n";
               $ajax .= "                    break;\n";
               $ajax .= "               }\n";
               $ajax .= "               if(\$key == \"save\")\n";
               $ajax .= "               {\n";
               $ajax .= "                    continue;\n";
               $ajax .= "               }\n";
               $sql = sprintf("SELECT campo,tipo,fixar FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-9)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if($myrow[1] == 'enum')
                              {
                                   $ret = checkEnum($db, $TABLE, $myrow[0]);
                                   if($ret == 1)
                                   {
                                        $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                        $ajax .= "               {\n";
                                        $ajax .= sprintf("                    \$sql .= \",%s='V'\";\n", strtoupper($myrow[0]));
                                        $ajax .= "                    continue;\n";
                                        $ajax .= "               }\n";
                                   }
                              }
                              if($myrow[1] == 'datetime' || $myrow[1] == 'date')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = sprintf(\"%s-%s-%s\",substr(\$val,6,4),substr(\$val,3,2),substr(\$val,0,2));\n";
                                   $ajax .= "               }\n";
                              }
                              if($myrow[1] == 'double' || $myrow[1] == 'float' || $myrow[1] == 'decimal')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    if(strstr(\$val, ',') != '')\n";
                                   $ajax .= "                    {\n";
                                   $ajax .= "                         \$val = str_replace('.', '', \$val);\n";
                                   $ajax .= "                    }\n";
                                   $ajax .= "                    \$val = str_replace(',', '.', \$val);\n";
                                   $ajax .= "                    \$val = abs(\$val);\n";
                                   $ajax .= "                    \$val = sprintf(\"%.2f\", floatval(\$val));\n";
                                   $ajax .= "               }\n";
                              }
                              if(strtolower($myrow[0]) == 'cnpj')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = str_replace(\".\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"/\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"-\",\"\",\$val);\n";
                                   $ajax .= "               }\n";
                              }
                              if(strtolower($myrow[0]) == 'cpf')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = str_replace(\".\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"/\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"-\",\"\",\$val);\n";
                                   $ajax .= "               }\n";
                              }
                              if(strtolower($myrow[0]) == 'rg')
                              {
                                   $ajax .= sprintf("               if(\$key == \"%s\")\n", strtoupper($myrow[0]));
                                   $ajax .= "               {\n";
                                   $ajax .= "                    \$val = str_replace(\".\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"/\",\"\",\$val);\n";
                                   $ajax .= "                    \$val = str_replace(\"-\",\"\",\$val);\n";
                                   $ajax .= "               }\n";
                              }
                         }
                    }
               }
               $ajax .= "               \$val = mysql_real_escape_string(\$val);\n";
               $ajax .= "               if(\$first == 0)\n";
               $ajax .= "               {\n";
               $ajax .= "                    \$sql .= \$key . \"='\".\$val.\"'\";\n";
               $ajax .= "               }\n";
               $ajax .= "               else\n";
               $ajax .= "               {\n";
               $ajax .= "                    \$sql .= \",\" . \$key . \"='\".\$val.\"'\";\n";
               $ajax .= "               }\n";
               $ajax .= "               \$first++;\n";
               $ajax .= "          }\n";

               $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-10)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if($myrow[1] == 'enum')
                              {
                                   $ret = checkEnum($db, $TABLE, $myrow[0]);
                                   if($ret == 1)
                                   {
                                        $ajax .= sprintf("          if(!isset(\$_REQUEST['%s']))\n", strtoupper($myrow[0]));
                                        $ajax .= "          {\n";
                                        $ajax .= sprintf("               \$sql .= \",%s='F'\";\n", strtoupper($myrow[0]));
                                        $ajax .= "          }\n";
                                   }
                              }
                         }
                    }
               }



               $ajax .= sprintf("          \$sql .= sprintf(\" WHERE id%s = '%%s'\",\$_REQUEST['save']);\n", strtolower($TABLE));
               //$ajax .= sprintf("          printf(\"<h3>%%s</h3>\n\", \$sql);\n");
               $ajax .= "          \$result = mysql_query(\$sql, \$db);\n";
               $ajax .= "          \$_REQUEST['ID'] = \$_REQUEST['save'];\n";
               $ajax .= sprintf("          if(file_exists(\"../includes/Cad%s_ajax_update.php\"))\n", ucfirst($TABLE));
               $ajax .= "          {\n";
               $ajax .= sprintf("               require(\"../includes/Cad%s_ajax_update.php\");\n", ucfirst($TABLE));
               $ajax .= "          }\n";
               $ajax .= "     }\n";
               $ajax .= "}\n";
               $ajax .= "else if(isset(\$_REQUEST['term']))\n";
               $ajax .= "{\n";
               $ajax .= "     if(\$_REQUEST['type'] == \"PESQUISA\")\n";
               $ajax .= "     {\n";
               if(substr($campo[0], 0, 2) == "id")
               {
                    $newcampo = GetCampo1($db, $campo[0]);
                    $tabela = substr($campo[0], 2, strlen($campo[0]) - 2);
                    if(strstr($tabela, "_id") != "")
                    {
                         $tb2 = strstr($tabela, "_id");
                         $tabela = substr($tabela, 0, strlen($tabela) - strlen($tb2));
                    }
                    $tabs = explode("_", $tabela);
                    if(count($tabs) > 1)
                    {
                         $tabela = "";
                         for($n = 0; $n < count($tabs); $n++)
                         {
                              if($n > 0)
                              {
                                   $tabela .= "_";
                              }
                              $tabs[$n] = ucfirst(strtolower($tabs[$n]));
                              $tabela .= $tabs[$n];
                         }
                    }
                    else
                    {
                         $tabela = ucfirst(strtolower($tabela));
                    }
                    $sql = sprintf("          if(is_numeric(\$_REQUEST['term']))\n");
                    $sql .= sprintf("          {\n");
                    $sql .= sprintf("               \$id = sprintf(\"%%d\", intval(\$_REQUEST['term']));\n");
                    $sql .= sprintf("               \$sql = \"SELECT %s.id%s,%s.%s,%s.%s FROM %s,%s WHERE %s.id%s=%s.id%s AND (%s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s.id%s LIKE _utf8 '\" . \$id . \"%%' collate utf8_unicode_ci)\";\n", ucfirst($TABLE), strtolower($TABLE), $tabela, $newcampo, ucfirst($TABLE), $campo[1], ucfirst($TABLE), $tabela, ucfirst($TABLE), strtolower($tabela), $tabela, strtolower($tabela), $tabela, $newcampo, ucfirst($TABLE), $campo[1], ucfirst($TABLE), strtolower($TABLE));
                    $sql .= sprintf("          }\n");
                    $sql .= sprintf("          else\n");
                    $sql .= sprintf("          {\n");
                    $sql .= sprintf("               \$sql = \"SELECT %s.id%s,%s.%s,%s.%s FROM %s,%s WHERE %s.id%s=%s.id%s AND (%s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' collate utf8_unicode_ci)\";\n", ucfirst($TABLE), strtolower($TABLE), $tabela, $newcampo, ucfirst($TABLE), $campo[1], ucfirst($TABLE), $tabela, ucfirst($TABLE), strtolower($tabela), $tabela, strtolower($tabela), $tabela, $newcampo, ucfirst($TABLE), $campo[1]);
                    $sql .= sprintf("          }\n");
               }
               else
               {
                    $sql = sprintf("          if(is_numeric(\$_REQUEST['term']))\n");
                    $sql .= sprintf("          {\n");
                    $sql .= sprintf("               \$id = sprintf(\"%%d\", intval(\$_REQUEST['term']));\n");
                    $sql .= sprintf("               \$sql = \"SELECT %s.id%s,%s.%s,%s.%s FROM %s WHERE %s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s.id%s LIKE _utf8 '\" . \$id . \"%%' collate utf8_unicode_ci\";\n", ucfirst($TABLE), strtolower($TABLE), ucfirst($TABLE), $campo[0], ucfirst($TABLE), $campo[1], ucfirst($TABLE), ucfirst($TABLE), $campo[0], ucfirst($TABLE), $campo[1], ucfirst($TABLE), strtolower($TABLE));
                    $sql .= sprintf("          }\n");
                    $sql .= sprintf("          else\n");
                    $sql .= sprintf("          {\n");
                    $sql .= sprintf("               \$sql = \"SELECT %s.id%s,%s.%s,%s.%s FROM %s WHERE %s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s.%s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' collate utf8_unicode_ci\";\n", ucfirst($TABLE), strtolower($TABLE), ucfirst($TABLE), $campo[0], ucfirst($TABLE), $campo[1], ucfirst($TABLE), ucfirst($TABLE), $campo[0], ucfirst($TABLE), $campo[1]);
                    $sql .= sprintf("          }\n");
               }
               $ajax .= $sql;
               $ajax .= "          \$result = mysql_query(\$sql, \$db);\n";
               $ajax .= "          printf(\"[\");\n";
               $ajax .= "          \$n = 0;\n";
               $ajax .= "          \$fields = mysql_num_fields(\$result);\n";
               $ajax .= "          while(\$myrow = mysql_fetch_row(\$result))\n";
               $ajax .= "          {\n";
               $ajax .= "               for(\$k=0;\$k<\$fields;\$k++)\n";
               $ajax .= "               {\n";
               //$ajax .= "                    \$myrow[\$k] = str_replace(\"%%\",\"%%%%\",\$myrow[\$k]);\n";
               $ajax .= "                    \$myrow[\$k] = str_replace(\"\\n\",\"+++\",\$myrow[\$k]);\n";
               $ajax .= "                    \$myrow[\$k] = str_replace(\"\\r\",\"---\",\$myrow[\$k]);\n";
               $ajax .= "                    \$myrow[\$k] = str_replace(\"\\\"\",\"\",\$myrow[\$k]);\n";
               //$ajax .= "                    \$myrow[\$k] = str_replace(\"'\",\"\",\$myrow[\$k]);\n";
               if(isset($_REQUEST['ISO']))
               {
                    $ajax .= "                    \$myrow[\$k] = iconv('iso-8859-1', 'utf-8', \$myrow[\$k]);\n";
               }
               $ajax .= "                    \$myrow[\$k] = trim(\$myrow[\$k]);\n";
               $ajax .= "               }\n";
               $ajax .= "               if(\$n == 0)\n";
               $ajax .= "               {\n";
               $ajax .= "                    echo \"{ \\\"id\\\": \\\"\$myrow[0]\\\", \\\"label\\\": \\\"(\$myrow[0])(\$myrow[2])\$myrow[1]\\\", \\\"value\\\": \\\"\$myrow[1]\\\" }\";\n";
               $ajax .= "               }\n";
               $ajax .= "               else\n";
               $ajax .= "               {\n";
               $ajax .= "                    echo \",{ \\\"id\\\": \\\"\$myrow[0]\\\", \\\"label\\\": \\\"(\$myrow[0])(\$myrow[2])\$myrow[1]\\\", \\\"value\\\": \\\"\$myrow[1]\\\" }\";\n";
               $ajax .= "               }\n";
               $ajax .= "               \$n++;\n";
               $ajax .= "          }\n";
               $ajax .= "          printf(\"]\\n\");\n";
               $ajax .= "     }\n";

               $sql = sprintf("SELECT campo,pesquisa FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-10)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if(substr(strtolower($myrow[0]), 0, 2) == "id" && $myrow[1] == "1")
                              {
                                   $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));

                                   $ajax .= "     if(\$_REQUEST['type'] == \"PESQ_" . strtoupper($myrow[0]) . "\")\n";
                                   $ajax .= "     {\n";
                                   $ajax .= "          \$id = sprintf(\"%d\", intval(\$_REQUEST['term']));\n";
                                   $campo0 = "";
                                   $campo1 = "";
                                   GetCampo2($db, $sel, $campo0, $campo1);
                                   $tabela = substr($myrow[0], 2, strlen($myrow[0]) - 2);
                                   $tabela = ucfirst($tabela);
                                   $sql = sprintf("          \$sql = \"SELECT %s,%s,%s FROM %s WHERE %s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s LIKE _utf8 '%%\" . \$_REQUEST['term'] . \"%%' OR %s LIKE _utf8 '\" . \$id . \"%%' collate utf8_unicode_ci\";\n", $myrow[0], $campo0, $campo1, ucfirst($sel), $campo0, $campo1, $myrow[0]);
                                   $ajax .= $sql;
                                   $ajax .= "          \$result = mysql_query(\$sql, \$db);\n";
                                   $ajax .= "          printf(\"[\");\n";
                                   $ajax .= "          \$n = 0;\n";
                                   $ajax .= "          \$fields = mysql_num_fields(\$result);\n";
                                   $ajax .= "          while(\$myrow = mysql_fetch_row(\$result))\n";
                                   $ajax .= "          {\n";
                                   //$ajax .= "               for(\$k=0;\$k<\$fields;\$k++)\n";
                                   //$ajax .= "               {\n";
                                   ////$ajax .= "                    \$myrow[\$k] = str_replace(\"%%\",\"%%%%\",\$myrow[\$k]);\n";
                                   //$ajax .= "                    \$myrow[\$k] = str_replace(\"\\n\",\"+++\",\$myrow[\$k]);\n";
                                   //$ajax .= "                    \$myrow[\$k] = str_replace(\"\\r\",\"---\",\$myrow[\$k]);\n";
                                   //$ajax .= "                    \$myrow[\$k] = str_replace(\"\\\"\",\"\",\$myrow[\$k]);\n";
                                   ////$ajax .= "                    \$myrow[\$k] = str_replace(\"'\",\"\",\$myrow[\$k]);\n";
                                   //if(isset($_REQUEST['ISO']))
                                   //{
                                   //     $ajax .= "                    \$myrow[\$k] = iconv('iso-8859-1', 'utf-8', \$myrow[\$k]);\n";
                                   //}
                                   //$ajax .= "                    \$myrow[\$k] = trim(\$myrow[\$k]);\n";
                                   //$ajax .= "               }\n";
                                   $ajax .= "               \$json = \"(\$myrow[0])(\$myrow[2])\$myrow[1]\";\n";
                                   $ajax .= "               \$json = json_encode(\$json);\n";
                                   $ajax .= "               if(\$n == 0)\n";
                                   $ajax .= "               {\n";
                                   $ajax .= "                    echo \"{ \\\"id\\\":\".json_encode(\$myrow[0]).\",\\\"label\\\":\$json, \\\"value\\\":\".json_encode(\$myrow[1]).\"}\";\n";
                                   $ajax .= "               }\n";
                                   $ajax .= "               else\n";
                                   $ajax .= "               {\n";
                                   $ajax .= "                    echo \",{\\\"id\\\":\".json_encode(\$myrow[0]).\",\\\"label\\\":\$json,\\\"value\\\":\".json_encode(\$myrow[1]).\"}\";\n";
                                   $ajax .= "               }\n";
                                   ///$ajax .= "               if(\$n == 0)\n";
                                   //$ajax .= "               {\n";
                                   //$ajax .= "                    printf(\"{ \\\"id\\\": \\\"\$myrow[0]\\\", \\\"label\\\": \\\"(\$myrow[0])(\$myrow[2])\$myrow[1]\\\", \\\"value\\\": \\\"\$myrow[1]\\\" }\");\n";
                                   //$ajax .= "               }\n";
                                   //$ajax .= "               else\n";
                                   //$ajax .= "               {\n";
                                   //$ajax .= "                    printf(\",{ \\\"id\\\": \\\"\$myrow[0]\\\", \\\"label\\\": \\\"(\$myrow[0])(\$myrow[2])\$myrow[1]\\\", \\\"value\\\": \\\"\$myrow[1]\\\" }\");\n";
                                   //$ajax .= "               }\n";
                                   $ajax .= "               \$n++;\n";
                                   $ajax .= "          }\n";
                                   $ajax .= "          printf(\"]\\n\");\n";
                                   $ajax .= "     }\n";
                              }
                         }
                    }
               }



               $ajax .= "}\n";
               $ajax .= "if(isset(\$_REQUEST['ID']))\n";
               $ajax .= "{\n";
               $ajax .= "if(!isset(\$_SESSION['IDFUNCIONARIO']))\n";
               $ajax .= "{\n";
               $ajax .= "     \$_SESSION['IDFUNCIONARIO'] = '-1';\n";
               $ajax .= "}\n";

               $select = sprintf("\$sql = \"SELECT ");

               $first = 0;
               $setDetail = 0;
               $chave = sprintf("id%s", strtolower($TABLE));
               $sql = sprintf("SELECT campo,tipo,tamanho,label,linha,coluna FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-11)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         $aba = "";
                         $tab = 1;
                         $linha = 0;
                         while($myrow = mysql_fetch_row($result))
                         {

                              $tipo = strtolower($myrow[1]);
                              $myrow[0] = strtoupper($myrow[0]);
                              $size = $myrow[2];
                              if($tipo == "mediumblob")
                              {
                                   if($first == 0)
                                   {
                                        $select .= sprintf("CONCAT('my_ajax/Cad%s_ajax.php?get%s=',id%s) as %s", $TABLE, $myrow[0], $TABLE, $myrow[0]);
                                   }
                                   else
                                   {
                                        $select .= sprintf(",CONCAT('my_ajax/Cad%s_ajax.php?get%s=',id%s) as %s", $TABLE, $myrow[0], $TABLE, $myrow[0]);
                                   }
                              }
                              else if($tipo == "date")
                              {
                                   if($first == 0)
                                   {
                                        $select .= sprintf("DATE_FORMAT(%s,'%%d/%%m/%%Y') as %s", $myrow[0], $myrow[0]);
                                   }
                                   else
                                   {
                                        $select .= sprintf(",DATE_FORMAT(%s,'%%d/%%m/%%Y') as %s", $myrow[0], $myrow[0]);
                                   }
                              }
                              else if(strstr($tipo, "double") != "" || strstr($tipo, "decimal") != "" || strstr($tipo, "float") != "")
                              {
                                   if($first == 0)
                                   {
                                        $select .= sprintf("REPLACE(REPLACE(REPLACE(FORMAT(%s, 2),'.','|'),',','.'),'|',',') as %s", $myrow[0], $myrow[0]);
                                   }
                                   else
                                   {
                                        $select .= sprintf(",REPLACE(REPLACE(REPLACE(FORMAT(%s, 2),'.','|'),',','.'),'|',',') as %s", $myrow[0], $myrow[0]);
                                   }
                              }
                              else if($tipo == "datetime")
                              {
                                   if($first == 0)
                                   {
                                        $select .= sprintf("DATE_FORMAT(%s,'%%d/%%m/%%Y %%H:%%i') as %s", $myrow[0], $myrow[0]);
                                   }
                                   else
                                   {
                                        $select .= sprintf(",DATE_FORMAT(%s,'%%d/%%m/%%Y %%H:%%i') as %s", $myrow[0], $myrow[0]);
                                   }
                              }
                              else
                              {
                                   if($first == 0)
                                   {
                                        $select .= sprintf("%s", $myrow[0]);
                                   }
                                   else
                                   {
                                        $select .= sprintf(",%s", $myrow[0]);
                                   }
                              }
                              $first = 1;
                         }
                         $select .= sprintf(",id%s as current,\".\$_SESSION['IDFUNCIONARIO'].\" as SESSION,id%s ", strtolower($TABLE), strtolower($TABLE));
                         $sx = $select;
                         $select .= sprintf("FROM %s", $TABLE);

                         $ajax .= "     if(\$_REQUEST['POS'] == \"first\")\n";
                         $ajax .= "     {\n";
                         $ajax .= sprintf("          %s ORDER BY id%s LIMIT 1\";\n", $select, strtolower($TABLE));
                         $ajax .= "     }\n";
                         $ajax .= "     else if(\$_REQUEST['POS'] == \"last\")\n";
                         $ajax .= "     {\n";
                         $ajax .= sprintf("          %s ORDER BY id%s DESC LIMIT 1\";\n", $select, strtolower($TABLE));
                         $ajax .= "     }\n";
                         $ajax .= "     else if(\$_REQUEST['POS'] == \"before\")\n";
                         $ajax .= "     {\n";
                         $ajax .= sprintf("          %s WHERE id%s<'\" . \$_REQUEST['ID'] . \"' ORDER BY id%s DESC LIMIT 1\";\n", $select, strtolower($TABLE), strtolower($TABLE));
                         $ajax .= "     }\n";
                         $ajax .= "     else if(\$_REQUEST['POS'] == \"after\")\n";
                         $ajax .= "     {\n";
                         $ajax .= sprintf("          %s WHERE id%s>'\" . \$_REQUEST['ID'] . \"' ORDER BY id%s LIMIT 1\";\n", $select, strtolower($TABLE), strtolower($TABLE));
                         $ajax .= "     }\n";
                         $ajax .= "     else if(\$_REQUEST['POS'] == \"remove\")\n";
                         $ajax .= "     {\n";
                         $ajax .= sprintf("          \$ret = check_remove(\$db, '%s', \$_REQUEST['ID']);\n", ucfirst($TABLE));
                         $ajax .= sprintf("          if(\$ret == '')\n");
                         $ajax .= sprintf("          {\n");
                         $ajax .= sprintf("               \$sql = \"DELETE FROM %s WHERE id%s = '\" . \$_REQUEST['ID'] . \"'\";\n", ucfirst($TABLE), strtolower($TABLE));
                         $ajax .= sprintf("               \$result = mysql_query(\$sql,\$db);\n");
                         $ajax .= sprintf("               \$sql = \"ALTER TABLE Funcionario AUTO_INCREMENT = 1\";\n");
                         $ajax .= sprintf("               \$result = mysql_query(\$sql,\$db);\n");
                         $ajax .= sprintf("          }\n");
                         $ajax .= sprintf("          else\n");
                         $ajax .= sprintf("          {\n");
                         $ajax .= sprintf("               \$message = \"Registro Utilizado na Tabela '\$ret', não é possível remove-lo.\";\n");
                         $ajax .= sprintf("               \$message = mysql_real_escape_string(\$message);\n");

                         $ajax .= sprintf("          }\n");
                         $ajax .= sprintf("          if(isset(\$message))\n");
                         $ajax .= sprintf("          {\n");
                         $ajax .= sprintf("               %s,'\$message' as message FROM %s WHERE id%s='\" . \$_REQUEST['ID'] . \"'\";\n",$sx,$TABLE, strtolower($TABLE));
                         $ajax .= sprintf("          }\n");
                         $ajax .= sprintf("          else\n");
                         $ajax .= sprintf("          {\n");
                         $ajax .= sprintf("               %s ORDER BY id%s DESC LIMIT 1\";\n", $select, strtolower($TABLE));
                         $ajax .= sprintf("          }\n");
                         $ajax .= "     }\n";
                         $ajax .= "     else\n";
                         $ajax .= "     {\n";
                         $ajax .= sprintf("          %s WHERE id%s='\" . \$_REQUEST['ID'] . \"'\";\n", $select, strtolower($TABLE));
                         $ajax .= "     }\n";
                         $ajax .= sprintf("     \$result = mysql_query(\$sql,\$db);\n");
                         $ajax .= sprintf("     if(mysql_num_rows(\$result) > 0)\n");
                         $ajax .= sprintf("     {\n");
                         $ajax .= sprintf("          \$rows = mysql_fetch_assoc(\$result);\n");
                         $ajax .= sprintf("          print json_encode(\$rows);\n");
                         $ajax .= sprintf("          return;\n");

                         $ajax .= sprintf("          printf(\"[\");\n");
                         $ajax .= sprintf("          \$fields = mysql_num_fields(\$result);\n");
                         $ajax .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                         $ajax .= sprintf("          {\n");
                         $ajax .= sprintf("               for(\$n=0;\$n<\$fields;\$n++)\n");
                         $ajax .= sprintf("               {\n");
                         //$ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"%%\",\"%%%%\",\$myrow[\$n]);\n");
                         $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"\\n\",\"+++\",\$myrow[\$n]);\n");
                         $ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"\\r\",\"---\",\$myrow[\$n]);\n");
                         //$ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"\\\"\",\"\",\$myrow[\$n]);\n");
                         //$ajax .= sprintf("                    \$myrow[\$n] = str_replace(\"'\",\"\",\$myrow[\$n]);\n");
                         if(isset($_REQUEST['ISO']))
                         {
                              $ajax .= sprintf("                    \$myrow[\$n] = iconv('iso-8859-1', 'utf-8', \$myrow[\$n]);\n");
                         }
                         $ajax .= sprintf("                    \$myrow[\$n] = trim(\$myrow[\$n]);\n");
                         $ajax .= sprintf("                    \$myrow[\$n] = urlencode(\$myrow[\$n]);\n");
                         $ajax .= sprintf("               }\n");
                    }
               }
               $first = 0;
               $sql = sprintf("SELECT campo,tipo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY aba_order,aba,Linha", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepAjax-12)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if(strstr($myrow[1], "double") != "" || strstr($myrow[1], "decimal") != "" || strstr($myrow[1], "float") != "")
                              {
                                   $ajax .= sprintf("               \$myrow[%d] = number_format(\$myrow[%d],2,',','.');\n", $first, $first);
                              }
                              if(strtolower($myrow[0]) == 'cnpj')
                              {
                                   $ajax .= sprintf("               \$myrow[%d] = str_replace('.','',\$myrow[%d]);\n", $first, $first);
                                   $ajax .= sprintf("               \$myrow[%d] = str_replace('-','',\$myrow[%d]);\n", $first, $first);
                                   $ajax .= sprintf("               \$myrow[%d] = str_replace('/','',\$myrow[%d]);\n", $first, $first);
                                   $ajax .= sprintf("               \$myrow[%d] = sprintf(\"%%s.%%s.%%s/%%s-%%s\",substr(\$myrow[%d],0,2),substr(\$myrow[%d],2,3),substr(\$myrow[%d],5,3),substr(\$myrow[%d],8,4),substr(\$myrow[%d],12,2));\n", $first, $first, $first, $first, $first, $first);
                              }
                              if($first == 0)
                              {
                                   if($myrow[1] == "mediumblob")
                                   {
                                        $ajax .= sprintf("               printf(\"{ \\\"id\\\": \\\"%s\\\", \\\"value\\\": \\\"my_ajax/Cad%s_ajax.php?get%s=\$myrow[\$fields-1]\\\" }\");\n", strtoupper($myrow[0]), ucfirst($TABLE), strtoupper($myrow[0]));
                                   }
                                   else
                                   {
                                        //$ajax .= sprintf("               printf(\"{ \\\"id\\\": \\\"%s\\\", \\\"value\\\": \\\"\$myrow[%d]\\\" }\");\n", strtoupper($myrow[0]), $first++);
                                        $ajax .= sprintf("               echo \"{ \\\"id\\\": \\\"%s\\\", \\\"value\\\": \\\"\".\$myrow[%d].\"\\\" }\";\n", strtoupper($myrow[0]), $first++);
                                   }
                              }
                              else
                              {
                                   if($myrow[1] == "mediumblob")
                                   {
                                        $ajax .= sprintf("               printf(\",{ \\\"id\\\": \\\"%s\\\", \\\"value\\\": \\\"my_ajax/Cad%s_ajax.php?get%s=%%s\\\" }\",\$myrow[\$fields-1]);\n", strtoupper($myrow[0]), ucfirst($TABLE), strtoupper($myrow[0]));
                                   }
                                   else
                                   {
                                        //$ajax .= sprintf("               printf(\",{ \\\"id\\\": \\\"%s\\\", \\\"value\\\": \\\"\$myrow[%d]\\\" }\");\n", strtoupper($myrow[0]), $first++);
                                        $ajax .= sprintf("               echo \",{ \\\"id\\\": \\\"%s\\\", \\\"value\\\": \\\"\".\$myrow[%d].\"\\\" }\";\n", strtoupper($myrow[0]), $first++);
                                   }
                              }
                         }
                    }
               }
               $ajax .= sprintf("               printf(\",{ \\\"id\\\": \\\"SESSION\\\", \\\"value\\\": \\\"%%s\\\" }\",\$_SESSION['IDFUNCIONARIO']);\n");
               $ajax .= sprintf("               if(isset(\$message))\n");
               $ajax .= sprintf("               {\n");
               $ajax .= sprintf("                    printf(\",{ \\\"id\\\": \\\"message\\\", \\\"value\\\": \\\"\$message\\\" },\");\n");
               $ajax .= sprintf("               }\n");
               $ajax .= sprintf("               printf(\",{ \\\"id\\\": \\\"current\\\", \\\"value\\\": \\\"\$myrow[%d]\\\" }\");\n", $first++);
               $ajax .= sprintf("          }\n");
               $ajax .= sprintf("          printf(\"]\");\n");

               $ajax .= sprintf("     }\n");
               $ajax .= "}\n";
               $ajax .= "mysql_close(\$db);";
               $ajax .= "?>\n";
               $dir = sprintf("./%s/my_ajax", $diretorio);
               if(!is_dir($dir))
               {
                    mkdir($dir);
               }
               $my_ajax = sprintf("%s/Cad%s_ajax.php", $dir, ucfirst($myrowF[0]));
               $fd = fopen($my_ajax, "w");
               if($fd)
               {
                    fprintf($fd, "%s", $ajax);
                    fclose($fd);
               }
          }
     }
}
?>
